#include "ResultModel.h"

ResultModel::ResultModel(QObject *parent)
    : QAbstractTableModel(parent)
{

}


QVariant ResultModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    if(role==Qt::DisplayRole){
        return hash[index.row()].value(index.column());
    }
    return QVariant();
}


QVariant ResultModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        if (orientation == Qt::Horizontal)
        {
            return m_header[section];
        }
    }

    return QVariant();
}


void ResultModel::replaceModel(QHash<int, QVariantList> value)
{
    beginResetModel();
    hash.clear();
    hash = value;
    endResetModel();
}


void ResultModel::insertRowData(int index,QVariantList data)
{
    beginInsertRows(QModelIndex(), hash.size(), hash.size());
    hash.insert(index,data);
    endInsertRows();
}

void ResultModel::appendRowData(QVariantList data)
{
    if(hash.isEmpty()){
        insertRowData(0,data);
    }else {
        insertRowData(hash.size(),data);
    }

}

void ResultModel::modifyData(int row,int column,QVariant data)
{
    if (row == -1 || column == -1){return;}
    beginResetModel();
    hash[row].replace(column,data);
    endResetModel();
}

void ResultModel::refresh()
{
    beginResetModel();
    endResetModel();
}




bool ResultModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    if (value != headerData(section, orientation, role)) {
        // FIXME: Implement me!
        emit headerDataChanged(orientation, section, section);
        return true;
    }
    return false;
}



bool ResultModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (data(index, role) != value) {
        // FIXME: Implement me!
        emit dataChanged(index, index, QVector<int>() << role);
        return true;
    }
    return false;
}

Qt::ItemFlags ResultModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEditable; // FIXME: Implement me!
}

bool ResultModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(parent, row, row + count - 1);
    // FIXME: Implement me!
    endInsertRows();
    return true;
}

bool ResultModel::insertColumns(int column, int count, const QModelIndex &parent)
{
    beginInsertColumns(parent, column, column + count - 1);
    // FIXME: Implement me!
    endInsertColumns();
    return true;
}

bool ResultModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    // FIXME: Implement me!
    endRemoveRows();
    return true;
}

bool ResultModel::removeColumns(int column, int count, const QModelIndex &parent)
{
    beginRemoveColumns(parent, column, column + count - 1);
    // FIXME: Implement me!
    endRemoveColumns();
    return true;
}

void ResultModel::slot_receivedModel(QStandardItemModel *model)
{
    int i,j;
    QStandardItem   *aItem;
    QList<QVariant> row;
    QHash<int,QVariantList> value;

    for (i=0;i<model->columnCount();i++)
    {
        row.push_back("0");
    }
    setHeader(row);

    row.clear();

    for ( i=0;i<model->rowCount();i++)
    {
        for( j=0;j<model->columnCount();j++)
        {
            aItem=model->item(i,j);
            row.push_back(aItem->text());
        }

        value.insert(i,row);
        row.clear();
    }
    replaceModel(value);

}



void ResultModel::test()
{
    QFile aFile("G:/MyApp/projectSelf/bin_release/resource/csv/Data.csv");


    QStringList fFileContent;
    if (aFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream aStream(&aFile);

        while (!aStream.atEnd())
        {
            QString str=aStream.readLine();//读取文件的一行
            fFileContent.append(str); //添加到 StringList
        }

        aFile.close();//关闭文件

    }
    QStandardItemModel *model =new QStandardItemModel();

    model->clear();
    int rowCnt=fFileContent.count(); //文本行数，第1行是标题
    model->setRowCount(rowCnt-1); //实际数据行数
    //设置表头
    QString header=fFileContent.at(0);//第1行是表头
    QStringList headerList=header.split(QRegExp(","),QString::SkipEmptyParts);

    //设置表格数据
    QString aText;
    QStringList tmpList;
    int j;
    QStandardItem   *aItem;
    for (int i=1;i<rowCnt;i++)
    {
        QString aLineText=fFileContent.at(i); //获取数据区的一行
        QStringList tmpList=aLineText.split(QRegExp(","),QString::SkipEmptyParts);
        for (j=0;j<headerList.count();j++)
        { //不包含最后一列
            aItem=new QStandardItem(tmpList.at(j));//创建item
            //aItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            model->setItem(i-1,j,aItem);
        }
    }

    return slot_receivedModel(model);
}
